<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;

use App\Models\Discount;

use App\Models\DiscountType;

use App\Models\Brand;

use App\Models\Category;

use App\Models\Goods;

use DB;

use App\Http\Requests\Admin\AdminDiscount\DiscountStoreRequest;

use App\Exceptions\Admin\DiscountException;

class DiscountController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Category $category)
    {

        //品牌数组

        $brandInfo = Brand::all();

        $brandArr = ['0'=>'选择品牌'];

        foreach ($brandInfo as $key => $value) {

            $brandArr[$value->brand_id] = $value->brand_name;

        }//end foreach()


        $search = ['discount_name'=>'', 'discount_type'=>'0'];

        $discountTypeArr = [0=>'选择类型', 1=>'全场通用', 2=>'商品', 3=>'分类', 4=>'店铺', 5=>'品牌'];


        $query = Discount::orderBy('created_at', 'desc');

        $discount_name = @$request->discount_name;

        if (@$discount_name) {

            $query = $query->where('discount_name', 'like', '%'.$discount_name.'%');

            $search['discount_name'] = $discount_name;

        }//end if()

        $discount_type = @$request->discount_type;

        if (@$discount_type) {

            $query = $query->where('discount_type', $discount_type);

            $search['discount_type'] = $discount_type;

        }//end if()

        $discounts = $query->paginate(10);

        return view('admin.discount.index', compact('discounts', 'search', 'discountTypeArr','category', 'brandArr'));
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Discount $discount, Category $category, Request $request)
    {
        $typeArr = [1=>'全场通用', 2=>'商品', 3=>'分类', 4=>'店铺', 5=>'品牌'];

        $Discounts = '';

        $search = ['name'=>'', 'type'=>'0'];

        //品牌数组
        $brandInfo = Brand::all();

        $brandArr = ['0'=>'选择品牌'];

        foreach ($brandInfo as $key => $value) {

            $brandArr[$value->brand_id] = $value->brand_name;

        }//end foreach()

        return view('admin.discount.create', compact('discount', 'search', 'typeArr','category', 'brandArr'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(DiscountStoreRequest $discountStoreRequest)
    {

        $use_id_str = @$discountStoreRequest->use_id_str;

        if ($discountStoreRequest->discount_type!=1 && empty($use_id_str)) {

            throw new DiscountException("类型的值不能为空");

        }//end if()


        DB::beginTransaction();//开启事务,开始批量操作

        try {

            $discount = Discount::create($discountStoreRequest->all());

            if (empty($discount)) {

                throw new DiscountException('限时折扣添加失败');

            }//end if()

            $discount_id = $discount->discount_id;

            if ($discountStoreRequest->discount_type!=1 && !empty($use_id_str)) {

                $use_id_arr = explode(',', $use_id_str);

                if (!empty($use_id_arr)) {

                    foreach ($use_id_arr as $key => $value) {

                        $use_id = $value;

                        $discount_type = DiscountType::create(['discount_id'=>$discount_id, 'use_id'=>$use_id]);

                        if (empty($discount_type)) {

                            throw new DiscountException('限时折扣类型值添加失败');

                        }//end if()

                    }//end foreach()

                }//end if()

            }//end if()

        } catch (Exception $e) {

            DB::rollback();

            throw new DiscountException('限时折扣添加失败');
        }

        DB::commit();

        return Redirect('admin/discount');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id, Category $category, Request $request)
    {
        $back_url = '';

        if($request->back_url){

            $back_url = $request->back_url;

        }//end if()


        $typeArr = [1=>'全场通用', 2=>'商品', 3=>'分类', 4=>'店铺', 5=>'品牌'];

        $discounts = Discount::findOrNew($id);

        $discount_id = $discounts->discount_id;

        $discount_type = DiscountType::where('discount_id', $discount_id)->get();

        $use_id_str = '';

        foreach ($discount_type as $key => $value) {

            $use_id = $value->use_id;

            if ($key==0) {

                $use_id_str = $use_id;
            }else{

                $use_id_str .= ','.$use_id;
            }//end if()

        }//end foreach()

        //品牌数组
        $brandInfo = Brand::all();

        $brandArr = ['0'=>'选择品牌'];

        foreach ($brandInfo as $key => $value) {

            $brandArr[$value->brand_id] = $value->brand_name;

        }//end foreach()

        $goods = [];

        if ($discounts->discount_type==2) {


            $discount_types = DiscountType::where('discount_id', $id)->get();

            //根据商品id查找商品是否存在
            foreach ($discount_types as $key => $value) {

                $goods_id = $value->use_id;

                $goodsInfo = Goods::find($goods_id);

                if ($goodsInfo) {

                    $goods[$key] = $goodsInfo->toArray();

                }

            }//end foreach()

        }//end if

        return view('admin.discount.edit', compact('discounts', 'typeArr', 'back_url', 'goods', 'category', 'brandArr', 'use_id_str'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(DiscountStoreRequest $discountStoreRequest, $discount_id)
    {
        $use_id_str = @$discountStoreRequest->use_id_str;

        if ($discountStoreRequest->discount_type!=1 && empty($use_id_str)) {

            throw new DiscountException("类型的值不能为空");
        }//end if()

        DB::beginTransaction();//开启事务,开始批量操作

        try {

            $discount=Discount::findOrNew($discount_id);

            $discount = $discount->update($discountStoreRequest->all());

            if (empty($discount)) {

                throw new DiscountException('限时折扣编辑失败');
            }//end if()

            $discount_type = DiscountType::where('discount_id', $discount_id)->delete();

            if ($discountStoreRequest->discount_type!=1 && !empty($use_id_str)) {

                $use_id_arr = explode(',', $use_id_str);

                foreach ($use_id_arr as $key => $value) {

                    $use_id = $value;

                    $discount_type = DiscountType::create(['discount_id'=>$discount_id, 'use_id'=>$use_id]);

                    if (empty($discount_type)) {

                        throw new DiscountException('限时折扣类型值添加失败');
                    }//end if()

                }//end foreach()

            }//end if()

        } catch (Exception $e) {

            DB::rollback();

            throw new DiscountException('限时折扣编辑失败');
        }

        DB::commit();

        $pre_url = @$discountStoreRequest->pre_url;

        if($pre_url){

            return Redirect($pre_url);
        }else{

            return Redirect('admin/discount');
        }//end if()

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        $return = ['status'=>1, 'message'=>'', 'data'=>''];

        $discount = Discount::find($id);

        $discount->delete();

        return $return;
    }
}
